﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBManagement.DAL;
using KBManagement.Library;
using System.Threading;
namespace KBManagement.OrderProcess
{
    class ProductItem
    {

        public Product product { get; set; }
        public Purchase_Order purchaseOrder { get; set; }
        public string UsedTime { get; set; }
        public bool Status { get; set; }
        Nullable<DateTime> ChangeTime { get; set; }
       
        public ProductItem(Product product)
        {
            
            this.product = product;
           
            try
            {
                purchaseOrder = product.Purchase_Orders.First(x => x.Status == false);
                
                Status = true;
            }
            catch (Exception)
            {

            }
        }
        public void ChangeProduct(ProductItem item)
        {
            item.CalculateAmount();
            this.purchaseOrder = item.purchaseOrder;
            this.purchaseOrder.Product = product;
            ChangeTime = DateTime.Now;
            Utils.CreateInstance().GetDBContext().SubmitChanges();
            item.purchaseOrder = null;
            item.Status = false;
            Status = true;
            //TODO
        }

        public void Start()
        {

            purchaseOrder = new Purchase_Order { CustomerID = null, Purchase_Amount = 0, ProductID = product.ID, Sale_Rate = 0, Status = false, StartTime = DateTime.Now, Total_Amount = 0 };
            product.Purchase_Orders.Add(purchaseOrder);

            Utils.CreateInstance().GetDBContext().SubmitChanges();
            Status = true;

        }
        public void SetCustomer(Customer cus)
        {
            if (cus != null)
            {
                purchaseOrder.Customer = cus;
                purchaseOrder.Sale_Rate = cus.Customer_Type.Rate;
            }
        }
        public void CalculateAmount()
        {

            TimeSpan tp = DateTime.Now.Subtract(purchaseOrder.StartTime.Value);
            UsedTime = tp.ToString();
            purchaseOrder.EndTime = DateTime.Now;
            purchaseOrder.Purchase_Date = purchaseOrder.EndTime.Value.Date;
            purchaseOrder.Status = true;
            if (ChangeTime != null)
            {
                TimeSpan afterChange = DateTime.Now.Subtract(ChangeTime.Value);
                purchaseOrder.Purchase_Amount = (product.Price * afterChange.Days * 24) + (product.Price * afterChange.Hours) + (product.Price * afterChange.Minutes / 60);
            }
            else
            {
                purchaseOrder.Purchase_Amount += (product.Price * tp.Days * 24) + (product.Price * tp.Hours) + (product.Price * tp.Minutes / 60);
            }
            purchaseOrder.Total_Amount += purchaseOrder.Purchase_Amount;
        }
        public void Stop()
        {
            if (purchaseOrder.Total_Amount == 0)
            {
                product.Purchase_Orders.Remove(purchaseOrder);
            }
            if (purchaseOrder.Customer != null)
            {
                purchaseOrder.Total_Amount -= purchaseOrder.Total_Amount * purchaseOrder.Sale_Rate/100;
                purchaseOrder.Customer.TotalMoney +=purchaseOrder.Total_Amount;
                var cusTypes = from ct in Utils.CreateInstance().GetDBContext().Customer_Types orderby ct.NormsMoney ascending select ct;
                foreach (var item in cusTypes)
                {
                    if (purchaseOrder.Customer.TotalMoney >= item.NormsMoney)
                    {
                        purchaseOrder.Customer.CustomeTypeID = item.ID;
                    }
                }
            }
            
            Utils.CreateInstance().GetDBContext().SubmitChanges();
            ChangeTime = null;
            Status = false;

        }
        public void AddService(Food food, int quantity)
        {
            if (purchaseOrder.Order_Details.Count > 0)
            {
                Order_Detail orderDetail = purchaseOrder.Order_Details.FirstOrDefault(x => x.FoodID == food.ID);
                if (orderDetail != null)
                {
                    orderDetail.Quantity += quantity;
                    orderDetail.Amount = (food.Price * orderDetail.Quantity);

                }
                else
                {
                    purchaseOrder.Order_Details.Add(new Order_Detail { FoodID = food.ID, Quantity = quantity, Amount = food.Price * quantity });
                }
            }
            else
            {
                purchaseOrder.Order_Details.Add(new Order_Detail { FoodID = food.ID, Quantity = quantity, Amount = food.Price * quantity });
            }
            purchaseOrder.Total_Amount += (food.Price * quantity);

            Utils.CreateInstance().GetDBContext().SubmitChanges();

        }
        public IQueryable GetServices()
        {
            var orders = from or in Utils.CreateInstance().GetDBContext().Order_Details where or.OrderID == purchaseOrder.ID select new { or.Food.Fo_Name, or.Quantity, or.Amount };
            return orders;
        }

        public IQueryable GetOrderDetail()
        {
            var orders = from or in Utils.CreateInstance().GetDBContext().Order_Details where or.OrderID == purchaseOrder.ID select or;
            return orders;
        }

        public void RemoveServices(object name, int quantity)
        {
            Order_Detail or = purchaseOrder.Order_Details.First(x => x.Food.Fo_Name.Equals(name));
            if (or != null)
            {
                decimal subprice = or.Food.Price * quantity;
                if (quantity == or.Quantity)
                {
                    purchaseOrder.Order_Details.Remove(or);
                    purchaseOrder.Total_Amount -= subprice;
                }
                else if (quantity < or.Quantity)
                {
                    or.Quantity -= quantity;
                    or.Amount -= subprice;
                    purchaseOrder.Total_Amount -= subprice;
                }


            }
            Utils.CreateInstance().GetDBContext().SubmitChanges();
        }

    }
}
